package com.xspace.android.xafcommon.db.dbsub;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.xspace.android.xafcommon.been.WorkItem;
import com.xspace.android.xafcommon.been.WorkUnDealForm;
import com.xspace.android.xafcommon.db.BaseSql;
import com.xspace.android.xafcommon.util.LogUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liuchen on 2016/9/20.
 */
public class WorkFormDBBiz extends BaseSql {

    private String tableName = "WorkForm";
    private SQLiteDatabase sqldb;
    private MyDBHelper helpDb;
    private String WorkType = "work_type";
    private String FormName = "form_name";
    private String UrlEdit = "form_urledit";
    private String UrlAdd = "form_urladd";
    private String UrlView = "form_urlview";
    private String IsRequired = "isrequired";
    private String IsMTimes = "is_manytimes";
    //存放userId
    private String Remark1 = "remark1";
    //formId
    private String Remark2 = "remark2";
    private String Remark3 = "remark3";

    private static final String TAG = "WorkFormDBBiz";

    protected WorkFormDBBiz() {
        super();
    }

    protected WorkFormDBBiz(MyDBHelper db) {
        super(db);
        helpDb = db;
    }

    @Override
    public void create(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + tableName
                + " (_ID INTEGER PRIMARY KEY AUTOINCREMENT," + WorkType
                + "  text," + FormName + " text," + UrlEdit + " text,"
                + UrlAdd + " text," + UrlView + " text ," + IsRequired + " text ," + IsMTimes + " text," + Remark1 +
                " text ," + Remark2 + " text ," + Remark3 + " text );");
    }

    @Override
    public void upgrade(SQLiteDatabase db) {

    }

    protected void checkDb() {
        if (sqldb == null || !sqldb.isOpen()) {
            sqldb = helpDb.getWritableDatabase();
        }
    }

    public List<WorkItem> queryRowsByWorkType(String workType, String userId) {
        LogUtil.d(TAG, "queryRowsByWorkType workType " + workType + " userID " + userId);
        checkDb();
        List<WorkItem> itemList = new ArrayList<WorkItem>();
        String selection = WorkType + "=? and " + Remark1 + " = ?";
        String[] selectionArgs = new String[]{workType, userId};
        String[] columns = {WorkType, FormName, UrlEdit, UrlAdd, UrlView, IsRequired, IsMTimes, Remark2};
        Cursor cur = sqldb.query(tableName, columns, selection, selectionArgs, null, null, null);
        if (cur != null) {
            while (cur.moveToNext()) {
                String formName = cur.getString(1);
                String urlEdit = cur.getString(2);
                String urlAdd = cur.getString(3);
                String urlView = cur.getString(4);
                String isRequired = cur.getString(5);
                String isMTimes = cur.getString(6);
                String formId = cur.getString(7);
                LogUtil.v(TAG, "queryRowsByWorkType workType:" + workType + " formName:" + formName);
                WorkItem wi = new WorkItem();
                wi.work_type = workType;
                wi.form_id = workType;
                wi.form_name = formName;
                wi.form_urledit = urlEdit;
                wi.form_urladd = urlAdd;
                wi.form_urlview = urlView;
                wi.isrequired = isRequired;
                wi.is_manytimes = isMTimes;
                wi.form_id = formId;
                itemList.add(wi);
            }
            cur.close();
        }
        return itemList;
    }

    public List<WorkUnDealForm> queryForm(String workType, String userId) {
        LogUtil.d(TAG, "queryRowsByWorkType workType " + workType + " userID " + userId);
        checkDb();
        List<WorkUnDealForm> itemList = new ArrayList<>();
        String selection = WorkType + "=? and " + Remark1 + " = ?";
        String[] selectionArgs = new String[]{workType, userId};
        String[] columns = {WorkType, FormName, UrlEdit, UrlAdd, UrlView, IsRequired, IsMTimes, Remark2};
        Cursor cur = sqldb.query(tableName, columns, selection, selectionArgs, null, null, null);
        if (cur != null) {
            while (cur.moveToNext()) {
                String formName = cur.getString(1);
                String urlEdit = cur.getString(2);
                String urlAdd = cur.getString(3);
                String urlView = cur.getString(4);
                String isRequired = cur.getString(5);
                String isMTimes = cur.getString(6);
                String formId = cur.getString(7);
                LogUtil.v(TAG, "queryRowsByWorkType workType:" + workType + " formName:" + formName);
                WorkUnDealForm wi = new WorkUnDealForm();
                wi.work_type = workType;
                wi.form_id = workType;
                wi.form_name = formName;
                wi.form_urledit = urlEdit;
                wi.form_urladd = urlAdd;
                wi.form_urlview = urlView;
                wi.isrequired = isRequired;
                wi.is_manytimes = isMTimes;
                wi.form_category = formId;
                itemList.add(wi);
            }
            cur.close();
        }
        return itemList;
    }

    public void updateAllRows(String workType, String userId, List<WorkItem> dataList) {
        checkDb();
        //清除之前的数据，再次插入新的数据
        selectionArgs = new String[]{workType, userId};
        selection = WorkType + "= ? and " + Remark1 + "=?";
        int result = sqldb.delete(tableName, selection, selectionArgs);
        LogUtil.d(TAG, "updateAllRows delete result " + result);
        values = new ContentValues();
        for (WorkItem workItem : dataList) {
            values.put(WorkType, workType);
            values.put(FormName, workItem.form_name);
            values.put(UrlEdit, workItem.form_urledit);
            values.put(UrlAdd, workItem.form_urladd);
            values.put(UrlView, workItem.form_urlview);
            values.put(IsRequired, workItem.isrequired);
            values.put(IsMTimes, workItem.is_manytimes);
            values.put(Remark1, userId);
            values.put(Remark2, workItem.form_id);
            long result2 = sqldb.insert(tableName, null, values);
            LogUtil.d(TAG, "updateAllRows insert result " + result2 + " formId " + workItem.form_id);
        }
    }

    public void insertOrUpdate(String workType, String userId, List<WorkUnDealForm> dataList) {
        checkDb();
        //清除之前的数据，再次插入新的数据
        selectionArgs = new String[]{workType, userId};
        selection = WorkType + "= ? and " + Remark1 + "=?";
        int result = sqldb.delete(tableName, selection, selectionArgs);
        LogUtil.d(TAG, "insertOrUpdate delete result " + result);
        values = new ContentValues();
        for (WorkUnDealForm workUnDealForm : dataList) {
            values.put(WorkType, workType);
            values.put(FormName, workUnDealForm.form_name);
            values.put(UrlEdit, workUnDealForm.form_urledit);
            values.put(UrlAdd, workUnDealForm.form_urladd);
            values.put(UrlView, workUnDealForm.form_urlview);
            values.put(IsRequired, workUnDealForm.isrequired);
            values.put(IsMTimes, workUnDealForm.is_manytimes);
            values.put(Remark1, userId);
            values.put(Remark2, workUnDealForm.form_category);
            long result2 = sqldb.insert(tableName, null, values);
            LogUtil.d(TAG, "insertOrUpdate insert result " + result2 + " form_category " + workUnDealForm.form_category);
        }
    }

    @Override
    protected synchronized int saveOrUpdate(String tableName) {
        return super.saveOrUpdate(tableName);
    }

    @Override
    protected synchronized int update(String tableName) {
        return super.update(tableName);
    }

    @Override
    protected int delete(String tableName) {
        return super.delete(tableName);
    }

    @Override
    protected int total(String table) {
        return super.total(table);
    }
}
